package com.demostic.demostic_test.utils;

import android.graphics.Bitmap;

import com.glidebitmappool.GlideBitmapPool;
import com.loopj.android.http.AsyncHttpClient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class BitmapUtils {
    public static byte[] bitmapToByte(Bitmap bitmap) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        byte[] bytes = out.toByteArray();
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static Bitmap getBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(AsyncHttpClient.DEFAULT_RETRY_SLEEP_TIME_MILLIS, AsyncHttpClient.DEFAULT_RETRY_SLEEP_TIME_MILLIS, Bitmap.Config.ARGB_8888);
        if (!bitmap.isMutable()) {
            return bitmap.copy(Bitmap.Config.ARGB_8888, true);
        }
        return bitmap;
    }

    public static void putBitmapMemory() {
        Bitmap bitmap = Bitmap.createBitmap(AsyncHttpClient.DEFAULT_RETRY_SLEEP_TIME_MILLIS, AsyncHttpClient.DEFAULT_RETRY_SLEEP_TIME_MILLIS, Bitmap.Config.ARGB_8888);
        if (!bitmap.isMutable()) {
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        }
        GlideBitmapPool.putBitmap(bitmap);
    }
}
